package org.qqcui.designpattern.patterns.factory.simplefactory.order;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.qqcui.designpattern.patterns.factory.simplefactory.pizza.Pizza;

/**
 * 订购披萨类
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2020-10-17 09:47
 **/
public class OrderPizza {

//	public OrderPizza() {
//		Pizza pizza = null;
//		String orderType = null;
//		do {
//			orderType = getType();
//			if ("greek".equals(orderType)) {
//				pizza = new GreekPizza();
//				pizza.setName("西班牙披萨");
//			} else if ("cheese".equals(orderType)) {
//				pizza = new CheesePizza();
//				pizza.setName("奶酪披萨");
//			} else if ("pepper".equals(orderType)) {
//				pizza = new PepperPizza();
//				pizza.setName("胡椒披萨");
//			} else {
//				break;
//			}
//			pizza.prepare();
//			pizza.bake();
//			pizza.cut();
//			pizza.box();
//		} while (true);
//	}

	// 采用简单工厂模式，OrderPizza类，只与简单pizza工厂发生关系即可
	// private PizzaSimpleFactory pizzaSimpleFactory;

	// public OrderPizza(PizzaSimpleFactory pizzaSimpleFactory) {
	// 	this.pizzaSimpleFactory = pizzaSimpleFactory;
	// 	orderPizza();
	// }

	public OrderPizza() {
		orderPizza();
	}

	/**
	 * 订购pizza
	 */
	private void orderPizza() {
		Pizza pizza = null;
		String orderType = null;
		do {
			System.out.println("简单工厂模式订购披萨");
			orderType = getType();
			pizza = PizzaSimpleFactory.createPizza(orderType);
			if (pizza == null) {
				// 订购失败
				System.out.println("披萨订购失败！");
				break;
			}
			pizza.prepare();
			pizza.bake();
			pizza.cut();
			pizza.box();
		} while (true);
	}

	public String getType() {
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入订购披萨类型：");
		try {
			return bufferedReader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
